import request from '@/utils/request';
import * as URL from '@/constants/api';
import * as util from '@/utils';
import dayjs from 'dayjs';

// 员工列表
export function getList (params) {
  // page & size
  return request({
    url: URL.EMPLOYEE,
    params,
  })
  .then(res => {
    res.data.data.rows = res.data.data.rows.map(d => ({
      ...d,
      timeOfEntry: dayjs(d.timeOfEntry).format('YYYY-MM-DD'),
      correctionTime: dayjs(d.correctionTime).format('YYYY-MM-DD'),
    }));
    return res.data.data;
  });
}

// 新增员工
export function add (data) {
  return request({
    url: URL.EMPLOYEE,
    method: 'post',
    data,
  })
  .then(res => res.data);
}

// 删除员工
export function remove (id) {
  return request({
    url: `${ URL.EMPLOYEE }/${ id }`,
    method: 'delete',
  })
  .then(res => res.data);
}

// 设置员工角色
export function setEmployeeRoles (data) {
  return request({
    url: URL.SET_EMPLOYEE_ROLES,
    method: 'put',
    data,
  })
  .then(res => res.data);
}

// 部门列表
export function getDepartmentList () {
  return request({
    url: URL.DEPARTMENT,
  })
  .then(res => util.getTreeData(res.data.data.depts.filter(d => d.pid !== '-1'), ''));
}

// 员工基本信息中的角色列表
export function getCheckedRoleIdList (id) {
  return request({
    url: `${ URL.EMPLOYEE }/${ id }`,
  })
  .then(res => res.data.data.roleIds);
}
